package com.yundasys.lab04springbootlocalmsg.config;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @PACKAGE_NAME: com.yundasys.lab04springbootlocalmsg.config
 * @NAME: RocketMqConfig
 * @USER: jiangta
 * @DATE: 2021/1/16
 * @TIME: 9:21 下午
 * @PROJECT_NAME: springboot-labs
 */
@Configuration
public class RocketMqConfig {

    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public DefaultMQProducer defaultMQProducer() {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
                DefaultMQProducer("paymentGroup");
        // Specify name server addresses.
        producer.setNamesrvAddr("10.19.160.88:9876");
        return producer;
    }

    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public DefaultMQPushConsumer defaultMQPushConsumer(@Qualifier("messageListener") MessageListenerConcurrently
                                                       messageListener) throws MQClientException {
        // Instantiate with specified consumer group name.
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");

        // Specify name server addresses.
        consumer.setNamesrvAddr("10.19.160.88:9876");

        // Subscribe one more more topics to consume.
        consumer.subscribe("payment", "*");
        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener(messageListener);

        return consumer;
    }
}
